<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Exception;//事务处理

class Proverify extends Auth
{
    public function __construct(Request $request){
        $this->getChildIDdata = [];
        parent::__construct($request);
        $this->data = $request->param();
        $this->user_id = session('userid') == null ? 1 : session('userid');
        $this->lang = isset($this->data['lang']) && $this->data['lang'] ? $this->data['lang'] : is_lang();
    }

    // 配件
    public function list(Request $request)
    {   
        if($request->isAjax()){
            $join = [
                ['multi_lang mu','zu.mkey = mu.mkey'],
                ['pro_brand b','zu.brand_id = b.id'],
                ['pro_img i','zu.id = i.pro_id'],   
            ];
            $field = ['zu.*,i.*,mu.content pro_name,b.name brand_name'];
            // 参数day存在则获取三天内发布的产品简介详情
            if(isset($this->data['day']) && $this->data['day']){
                $time = time() - (3*24*60*60);
                $prodesc = getDatas('prodesc',['*'],['pro_id'=>['>',0],'status'=>1,'verify'=>['in','2,3'],'prodesc_cat_id'=>['>',0],'create_at'=>['>',$time],'lang'=>$this->lang]);
            }else{
                $prodesc = getDatas('prodesc',['*'],['pro_id'=>['>',0],'status'=>1,'verify'=>1,'prodesc_cat_id'=>['>',0],'lang'=>$this->lang]);
            }
            // dump($prodesc);die;
            if(!empty($prodesc)){
                $prodesc_cat = array();
                foreach ($prodesc as $k=> $v) {
                    // 获取相应的介绍类型
                    $prodesc_cat[$k] = getSearch('prodesc_cat','zu',['zu.*,mu.content prodesc_cat_name'],['zu.id'=>$v['prodesc_cat_id'],'mu.lang'=>$this->lang],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc')[0];
                    $pro = getSearch('pro','zu',$field,['zu.id'=>$v['pro_id'],'mu.lang'=>$this->lang],$join,'zu.id desc');
                    // dump($pro);die;
                    $prodesc_cat[$k]['pro_id'] = $pro ? $pro[0]['id'] :0;
                    $prodesc_cat[$k]['pro_name'] = $pro ? $pro[0]['pro_name'] :'';
                    $prodesc_cat[$k]['brand_name'] = $pro ? $pro[0]['brand_name'] :'';
                    $prodesc_cat[$k]['pro_img'] = $pro ? $pro[0]['img_url'] :'';

                    $prodesc_cat[$k]['lang_name'] = getNoticLang($v['lang']);
                    $prodesc_cat[$k]['lang'] = $v['lang'];
                    $prodesc_cat[$k]['create_name'] = getinfo($v['create_id'], 'admin')['nickname'];//编辑者
                    $prodesc_cat[$k]['create_at'] = getTimeDates($v['create_at'],$this->lang);
                    $prodesc_cat[$k]['prodesc_id'] = $v['id'];
                    $prodesc_cat[$k]['verify'] = $v['verify'];
                    $prodesc_cat[$k]['prochar_id'] = $v['prochar_id'];
                    $prodesc_cat[$k]['prochar_model'] = getValue('prochar',['id'=>$v['prochar_id']],'model');
                }
            }else{
                $prodesc_cat = [];
            }
            $returnData['size'] = count( $prodesc_cat);
            // 获取分页数据
            if(isset($this->data['page']) && isset($this->data['limit'])){
                if($this->data['limit']*($this->data['page']-1) < $returnData['size']){
                    $prodesc_cat = array_slice($prodesc_cat,$this->data['limit']*($this->data['page']-1),$this->data['limit']);
                    $returnData['page'] = $this->data['page'];
                }
            }else{
                $returnData['page'] =1;
            }
            // dump($prodesc_cat);die;
            $returnData['prodesc_cat'] = $prodesc_cat;
            return json_encode($returnData);
            
        }else{
            return view('list');
        }
    }

    public function verifyopen(){
        // dump($this->data['lang']);die;
        if(isset($this->data['request']) && $this->data['request'] == 'verify'){
            if(isset($this->data['id']) && $this->data['id']){
                // $prodesc_catname = '';
                $proname = '';
                foreach ($this->data['id'] as $k => $v) {
                    if($k<3){
                        $prodesc = getDatas('prodesc',['*'],['id'=>$v]);
                        $proname .= ','.getValue('multi_lang',['mkey'=>'pro_'.$prodesc[0]['pro_id'],'lang'=>$this->lang],'content');
                    }
                    if($k = 3){
                        $proname.= '...';
                    } 
                }
                ltrim($proname,',');
                // ltrim($prodesc_catname,',');
                if(isset($this->data['retract']) && $this->data['retract'] == 'true'){
                    // 撤回

                    foreach ($this->data['id'] as $k => $v) {
                        $res = updateData('prodesc',['id'=>$v],['verify'=>1]);
                    }
                    if($res){
                        return json_encode(array('status'=>1,'msg'=>lang('introduction_type').'：'.$proname.lang('withdraw').' '.lang('successfully')));
                    }else{
                        return json_encode(array('status'=>0,'msg'=>lang('introduction_type').'：'.$proname.llang('withdraw').' '.lang('failed')));
                    }
                }else{
                    // return $this->data['id'];
                    // 审核
                    foreach ($this->data['id'] as $k => $v) {
                        $res = updateData('prodesc',['id'=>$v],['verify'=>2,'verified_id'=>$this->user_id,'verified_at'=>time()]);
                    }
                    if($res){
                        return json_encode(array('status'=>1,'msg'=>lang('introduction_type').'：'.$proname.' '.lang('review').' '.lang('successfully')));
                    }else{
                        return json_encode(array('status'=>0,'msg'=>lang('introduction_type').'：'.$proname.' '.lang('review').' '.lang('failed')));
                    }
                }
                
            }else{
                return json_encode(array('status'=>2,'msg'=>lang('improve_submission_data')));
            }
        }else{
            if(isset($this->data['prodesc_id']) && $this->data['prodesc_id']){
                // return isset($this->data['lang']) ? is_lang('en') : is_lang('en');
                $prodesc = getDatas('prodesc',['*'],['id'=>$this->data['prodesc_id'],'lang'=>$this->lang,'verify'=>1])[0];
                $prodesc_cat = getSearch('prodesc_cat','zu',['mu.content'],['zu.id'=>$prodesc['prodesc_cat_id'],'mu.lang'=>$this->lang],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc')[0];
                $prodesc['prodesc_cat_name'] = $prodesc_cat['content'];
                $prodesc['lang_name'] = getNoticLang($prodesc['lang']);
                return view('verifyopen',['prodesc'=>$prodesc]);
            }
        }
    }

    public function editopen(){
        if(isset($this->data['request']) && $this->data['request'] == 'edit'){

        }else{
            return view('verifyopen',['title'=>lang('add_product_properties')]);
        }
    }

}